package com.ztgf.message.api.client;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.message.model.bo.MessageAnnouncementBO;
import com.ztgf.message.model.dto.MessageAnnouncementDTO;
import com.ztgf.message.model.service.IMessageAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 通知消息控制器
 *
 * @author YUE
 * @Date 2020-05-28 16:20:53
 */

@RestController
@RequestMapping("/api/messageAnnouncement")
@Slf4j
@Api(value = "MessageAnnouncementApiController",tags = "通知消息Api接口")
public class MessageAnnouncementApiController {

    @DubboReference
    private IMessageAnnouncementService messageAnnouncementService;

    @PostMapping("/getUnreadMessageNum")
    @ApiOperation(value = "获取未读消息数量",notes = "获取未读消息数量")
    public Result<Integer> getUnreadMessageNum(Long shopId,int type){
        if(null == shopId){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        Integer unreadMessageNum = messageAnnouncementService.getUnreadMessageNum(shopId,type);
        return new Result<>(unreadMessageNum);
    }

    @PostMapping("/getUnreadMessage")
    @ApiOperation(value = "获取未读消息",notes = "获取未读消息")
    public Result< List<MessageAnnouncementDTO>> getUnreadMessage(Long shopId,int type){
        if(null == shopId){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<MessageAnnouncementDTO> unreadMessageList = messageAnnouncementService.getUnreadMessage(shopId,type);
        return new Result<>(unreadMessageList);
    }

    /**
     * 更新所有消息状态为已读
     * @param messageAnnouncementBO
     * @return
     */
    @PostMapping("/updateAllMessageStatus")
    @ApiOperation(value = "更新所有消息状态",notes = "更新所有消息状态")
    public Result updateAllMessageStatus(MessageAnnouncementBO messageAnnouncementBO){
        if(null == messageAnnouncementBO){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        if(null == messageAnnouncementBO.getShopId())
            return new Result<>(CodeEnum.FAIL_100002 );
        if(null == messageAnnouncementBO.getType())
            return new Result<>(CodeEnum.FAIL_100002 );
        if(null == messageAnnouncementBO.getShopType())
            return new Result<>(CodeEnum.FAIL_100002 );
        messageAnnouncementService.updateAllMessageStatusByIds(messageAnnouncementBO.getShopId(),messageAnnouncementBO.getType(),messageAnnouncementBO.getShopType());
        return new Result();
    }

    /**
     * 删除已读取的消息
     * @param id
     * @return
     */
    @PostMapping("/deleteMessageById")
    @ApiOperation(value = "删除选中消息",notes = "删除选中消息")
    public Result deleteMessageById(Long id){
        if(null == id){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        messageAnnouncementService.deleteSelectMessageById(id);
        return new Result();
    }

    /**
     * 删除已读取的消息
     * @param messageAnnouncementBO
     * @return
     */
    @PostMapping("/deleteAllMessageStatus")
    @ApiOperation(value = "删除所有消息已读",notes = "删除所有消息已读")
    public Result deleteAllMessageStatus(MessageAnnouncementBO messageAnnouncementBO){
        if(null == messageAnnouncementBO){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        if(null == messageAnnouncementBO.getShopId())
            return new Result<>(CodeEnum.FAIL_100002 );
        if(null == messageAnnouncementBO.getType())
            return new Result<>(CodeEnum.FAIL_100002 );
        if(null == messageAnnouncementBO.getShopType())
            return new Result<>(CodeEnum.FAIL_100002 );
        messageAnnouncementService.deleteAllMessageStatusByIds(messageAnnouncementBO.getShopId(),messageAnnouncementBO.getType(),messageAnnouncementBO.getShopType());
        return new Result();
    }

    /**
     * 消息的Id
     * @param id
     * @return
     */
    @PostMapping("/updateMessageStatus")
    @ApiOperation(value = "更新消息状态",notes = "更新消息状态")
    public Result updateMessageStatus(Long id){
        if(null == id){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        boolean b = messageAnnouncementService.updateMessageStatusById(id);
        return new Result();
    }

    @ApiOperation(value = "删除通知消息",notes = "删除通知消息")
    @ApiImplicitParam(name = "id",value = "公告消息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        if(null == id)
            return new Result<>(CodeEnum.FAIL_100002 );
        boolean flag = messageAnnouncementService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<MessageAnnouncementDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        MessageAnnouncementDTO dto = messageAnnouncementService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "通知消息列表",notes = "通知消息列表")
    @PostMapping("/queryPageListByShopId")
    public Result<Page<MessageAnnouncementDTO>> queryPageListByShopId(MessageAnnouncementBO bo){
        Page<MessageAnnouncementDTO> page = messageAnnouncementService.getBySelectBannerInfoPage(bo);
        return new Result<>(page);
    }
}
